var Rosy = {
	Version : '0.3.1',
	setHTMLStyles :  function(htmlElement,props){
		for(prop in props){
			htmlElement.style[prop] = props[prop];
		}
	},
	mappingObj : function(obj,mappingObj){
		var rtnObj = new Object();
		for(prop in obj){
			rtnObj[mappingObj[prop]] = obj[prop];
		}
		return rtnObj;
	},
	mappingObjArray : function (objArray,mappingObj){
		var rtnObjArray = new Array();
		for(var i = 0;i<objArray.length;i++){
			rtnObjArray.push(this.mappingObj(objArray[i],mappingObj));
		}
		return rtnObjArray;
	},
	stringToNumberFormat : function (_in){
		if(_in.match(/\d+/g)==null) return '';
		var f = _in.match(/^[-]/)?'-':'';
		var u = _in.match(/[.][0-9]+/);
		if(u == null) u = '';
		var t = _in.match(/[0-9,]+[\.]/)?_in.match(/[0-9,]+[\.]/)+'':_in ;
		t = t.match(/\d/g).join('');
		t = t - 0 + '';
		t = t.match(/\d+/g).join('');
		var a = new Array();
		var b = new Array();
		for(var i = 0;i<t.length;i++){
			if(i!=0 && i%3 == 0){
			a.push(b.reverse().join(''));
			b = new Array();
			}
			b.push(t.substr(t.length-i-1,1));
		}
		a.push(b.reverse().join(''));
		return f + a.reverse().join(',') + u;
	},
	
	padZero : function(_in,len,right){
		if(_in.length > len) return _in;
		var z = '';
		for(var i = 0;i < len - _in.length ; i++){
			z += '' + '0';
		}
		if(right){
			return _in + z;
		}else{
			return z + _in;
		}
	},
	
	padLeftZero : function(_in,len){
		return this.padZero(_in,len,false);
	},
	
	padRightZero : function(_in,len){
		return this.padZero(_in,len,true);
	}

}
/**---------- Logging Class --*/
function RosyLog(){
	this.use = false;
	this.lineNumber = 0;
	
	this.logWindowStyle={"width":"700px","textAlign":"left"};
	this.logConsoleBaseStyle={"width":"600px","height":"200px","overflow":"auto","border":"solid 1px black","padding":"2px","backgroundColor":"#FFFFFF"};
	this.logLineStyle = {"margin":"0px"};

	this.logConsoleTextStyle = {"fontSize":"10pt","margin":"0px","fontFamily":"monospace","textAlign":"left","lineHeight":"1.2","textIndent":"0","padding":"0px","backgroundColor":"#FFFFFF"};
	this.logConsoleExecuteTextStyle = {"color":"blue","fontWeight":"bold"};
	this.logConsoleObjectTextStyle = {"color":"green"};
	this.logConsoleErrorTextStyle = {"color":"red"};
	this.logConsoleCommandResultTextStyle = {"color":"blue"};

	this.logConsoleExecuteInputStyle = {"fontFamily":"monospace","fontSize":"10pt","margin":"3px"};

	this.clearLogWindow = function(){
		var w = document.getElementById('rosyLogWindowConsole');
		while(w.hasChildNodes()){
			w.removeChild(w.firstChild);			
		}
		wl = document.createElement('p');
		wl.id= 'rosyLogWindowConsoleLastLine';
		w.appendChild(wl);
	};
	
	this.debug = function(){
		if(!(this.use)) return;
		try{
		var w ;
		var wc;
		var wl;
		w = document.getElementById('rosyLogWindow');
		wc = document.getElementById('rosyLogWindowConsole');
		wl = document.getElementById('rosyLogWindowConsoleLastLine');
		if(!w || !wc){
			w = document.createElement('div');
			w.id = 'rosyLogWindow';
			Rosy.setHTMLStyles(w,this.logWindowStyle);
			wc = document.createElement('div');
			wc.id = 'rosyLogWindowConsole';
			wl = document.createElement('p');
			wl.id= 'rosyLogWindowConsoleLastLine';
			Rosy.setHTMLStyles(wl,this.logConsoleTextStyle);
			wl.style.height = '0px';
			wc.appendChild(wl);
			var executeFunction = new Function('rosyLogConsole.execute(document.getElementById("rosyLogConsoleInput").value);void(0);return false;');
			var fm = document.createElement('form');
			fm.name = 'rosyLogForm';
			fm.onsubmit = executeFunction
			var inp = document.createElement('input');
			inp.type = 'text';
			inp.size = '60';
			inp.id = 'rosyLogConsoleInput';
			if(navigator.userAgent.indexOf("MSIE") >= 0) inp.onkeydown = new Function('rosyLogConsole.keyEventHandler();');
			Rosy.setHTMLStyles(inp,this.logConsoleExecuteInputStyle);
			var inpb = document.createElement('input');
			inpb.type='button';
			inpb.value = 'execute';
			inpb.onclick = executeFunction;
			fm.appendChild(inp);
			fm.appendChild(inpb);
			w.appendChild(wc);
			w.appendChild(fm);
			document.body.appendChild(w);
			if(navigator.userAgent.indexOf("MSIE") < 0) inp.addEventListener('keydown',function(e){rosyLogConsole.keyEventHandler(e)},true);
		}
		Rosy.setHTMLStyles(wc,this.logConsoleBaseStyle);
	    
    	var messages = new Array();
		for(var i = 0;i<arguments.length;i++){
			var _in = arguments[i];
			try{console.log(_in)}catch(e){};
			if(_in == null){
				messages.push({"t":"n","text":""});
			}else if(_in instanceof Array){
				messages.push({"t":"o","text":"(ARRAY) ["+_in.join(',')+"]"});
			}else if(_in instanceof Error){
				messages.push({"t":"e","text":"(ERROR)"});
				for(p in _in){
					messages.push({"t":"e","text":p + ' : ' + _in[p]});
				}
			}else if( typeof _in == 'object'){
				messages.push({"t":"o","text":"(OBJECT)"});
				for(p in _in){
					messages.push({"t":"o","text":p + ' : ' + _in[p]});
				}
			}else{
				messages.push({"t":"n","text":_in});
			}
		}
	    var logtime = this.getLogTime();
		var i = 0;
		var p = document.createElement('p');
		Rosy.setHTMLStyles(p,this.logLineStyle);
		for(var m = 0; m < messages.length;m++){
			var p2 = document.createElement('pre');
			Rosy.setHTMLStyles(p2,this.logConsoleTextStyle);
			var message = '';
			if(i==0){ 
				message = logtime + ' : ' +messages[m].text
			}else{
				message = messages[m].text
			};
			if(messages[m].t == 'e'){
				Rosy.setHTMLStyles(p2,this.logConsoleErrorTextStyle);
			}else if(messages[m].t == 'o'){
				Rosy.setHTMLStyles(p2,this.logConsoleObjectTextStyle);
			}
			var t = document.createTextNode(message);
			p2.appendChild(t);
			p.appendChild(p2);
			i++;
		}
    	wc.insertBefore(p,wl);
		wl.scrollIntoView();
		}catch(e){
			try{
				console.log('error has occured in rosy.js Log() ');
				console.log(e);
			}catch(e){}
		}
	};
	
	this.d = this.debug;
	
	this.getLogTime = function(){
    	var d = new Date();
		return d.getFullYear() + '-' + Rosy.padLeftZero(''+(d.getMonth()+1),2) 
			+ '-' + Rosy.padLeftZero(''+d.getDate(),2) + ' ' + Rosy.padLeftZero(''+d.getHours(),2) + ':' 
			+ Rosy.padLeftZero(''+d.getMinutes(),2) + '.' + Rosy.padLeftZero(''+d.getSeconds(),2) 
			+ Rosy.padRightZero(''+d.getMilliseconds(),3) + ' ';
	};

}
var rosyLogConsole = {
	rlog : new RosyLog(),
	commandLog : [],
	addCmmandLog : function (_com){
		if(!_com) return;
		if(!this.commandLog || this.commandLog[this.commandLog.length -1] != _com) this.commandLog.push(_com);
		this.commandLogCursol = this.commandLog.length;
	},
	clearCommandLine : function (){if(document.getElementById("rosyLogConsoleInput")) document.getElementById("rosyLogConsoleInput").value = '';},
	execute : function(_in){
		try{
			this.rlog.use = true;
			var p = document.createElement('p');
			Rosy.setHTMLStyles(p,this.rlog.logLineStyle);
			var p2 = document.createElement('p');
			Rosy.setHTMLStyles(p2,this.rlog.logConsoleTextStyle);
			Rosy.setHTMLStyles(p2,this.rlog.logConsoleExecuteTextStyle);
			var t = document.createTextNode('> ' + _in);
			p2.appendChild(t);
			p.appendChild(p2);
			var wl = document.getElementById('rosyLogWindowConsoleLastLine');
			document.getElementById('rosyLogWindowConsole').insertBefore(p,wl);
			wl.scrollIntoView();
			this.addCmmandLog(_in);
			if(this.isCommand(_in)==false){
				window.document.location.href = 'javascript:try{' + _in + ';void(0);}catch(e){rlog.d(e)}';
			}
		}catch(e){
			this.rlog.debug(e);
		};
		this.clearCommandLine();
	},
	createCommandResult :function(messages){
		var wl = document.getElementById('rosyLogWindowConsoleLastLine');
		var wc = document.getElementById('rosyLogWindowConsole');
		var p = document.createElement('p');
		Rosy.setHTMLStyles(p,this.rlog.logLineStyle);
		for(var m = 0; m < messages.length;m++){
			p.appendChild(messages[m]);
		}
    	wc.insertBefore(p,wl);
		wl.scrollIntoView();
	},
	commandLogCursol : 0,
	keyEventHandler : function(event){
		var out = document.getElementById("rosyLogConsoleInput");
		var keyCode
		try{
			keyCode = window.event.keyCode;
		}catch(e){
			keyCode = event.keyCode;
		}
		if(keyCode == 38 && this.commandLogCursol > 0){
			this.commandLogCursol --;
			out.value = this.commandLog[this.commandLogCursol];
		}else if(keyCode == 40 && this.commandLogCursol < this.commandLog.length ){
			this.commandLogCursol ++;
			out.value = (a = this.commandLog[this.commandLogCursol])?a:'';
		}else if(keyCode == 37 && out.value.length == 0){
			out.value = 'rlog.d(\'\')';
		}else if(keyCode == 39 && out.value.length == 0){
			out.value = 'rlog.d()';
		}
	},
	isCommand : function(_in){
		if(_in.indexOf('#') != 0) return false;
		if(_in == '#list'){
			var messages = new Array();
			for(var i = 0;i<this.commandLog.length;i++){
				var p2 = document.createElement('p');
				Rosy.setHTMLStyles(p2,this.rlog.logConsoleTextStyle);
				Rosy.setHTMLStyles(p2,this.rlog.logConsoleCommandResultTextStyle);
				p2.appendChild(document.createTextNode('['+i+']:'+this.commandLog[i]));
				messages.push(p2);
			}
			this.createCommandResult(messages);

		}else if(_in == '#help'){
			var messages = new Array();
			for(cat1 in this.helpmessage){
				var p2 = document.createElement('p');
				Rosy.setHTMLStyles(p2,this.rlog.logConsoleTextStyle);
				Rosy.setHTMLStyles(p2,this.rlog.logConsoleCommandResultTextStyle);
				p2.style.fontWeight = 'bold';
				p2.appendChild(document.createTextNode(cat1));
				messages.push(p2);
				for(cat2 in this.helpmessage[cat1]){
					p2 = document.createElement('p');
					Rosy.setHTMLStyles(p2,this.rlog.logConsoleTextStyle);
					Rosy.setHTMLStyles(p2,this.rlog.logConsoleCommandResultTextStyle);
					p2.appendChild(document.createTextNode(cat2 + ' : ' + this.helpmessage[cat1][cat2]));
					messages.push(p2);
				}
			}
			this.createCommandResult(messages);

		}else if(_in == '#img'){
			var img = document.getElementsByTagName('img');
			var messages = new Array();
			var checkObj = new Object();
			for(var i = 0;i<img.length;i++){
				if(!checkObj[img[i].src]){
					var p2 = document.createElement('p');
					p2.onmouseover = new Function('rosyLogConsole.showImgToolTip()');
					p2.onmouseover = new Function('rosyLogConsole.deleteImgToolTip()');
					Rosy.setHTMLStyles(p2,this.rlog.logConsoleTextStyle);
					Rosy.setHTMLStyles(p2,this.rlog.logConsoleCommandResultTextStyle);
					p2.appendChild(document.createTextNode(img[i].src));
					messages.push(p2);
					checkObj[img[i].src] = 'a';
				}
			}
			checkObj = null;
			this.createCommandResult(messages);
		}else if(_in == '#css'){
			var links = document.getElementsByTagName('link');
			var messages = new Array();
			for(var i = 0;i<links.length;i++){
				if(links[i]['type'] && links[i]['type'].indexOf('css')>0){
					var p2 = document.createElement('p');
					Rosy.setHTMLStyles(p2,this.rlog.logConsoleTextStyle);
					Rosy.setHTMLStyles(p2,this.rlog.logConsoleCommandResultTextStyle);
					p2.appendChild(document.createTextNode(links[i]['href']));
					messages.push(p2);
				}
			}
			this.createCommandResult(messages);
		}else if(_in == '#js'){
			var scripts = document.getElementsByTagName('script');
			var messages = new Array();
			var checkObj = new Object();
			for(var i = 0;i<scripts.length;i++){
				if(scripts[i]['src'] && scripts[i]['type'] && scripts[i]['type'].indexOf('javascript')>0){
					if(!checkObj[scripts[i]['src']]){
						var p2 = document.createElement('p');
						Rosy.setHTMLStyles(p2,this.rlog.logConsoleTextStyle);
						Rosy.setHTMLStyles(p2,this.rlog.logConsoleCommandResultTextStyle);
						p2.appendChild(document.createTextNode(scripts[i]['src']));
						messages.push(p2);
						checkObj[scripts[i]['src']] = 'a';
					}
				}
			}
			checkObj = null;
			this.createCommandResult(messages);
		}else if(_in.indexOf('#lib ')==0){
			var apscript = function(url,eid){
				var s = document.createElement('script');
				s.type = 'text/javascript';
				s.id = eid;
				s.src = url;
				document.body.appendChild(s);
			}
			if(_in.indexOf(' jquery') >= 4) apscript('http://jqueryjs.googlecode.com/files/jquery-1.2.2.min.js','rosy_jquery');
			if(_in.indexOf(' prototype') >= 4) apscript('http://www.prototypejs.org/assets/2007/11/6/prototype.js','rosy_prototype');

		}else if(_in == '#clear'){
			this.rlog.clearLogWindow();
		}else if(_in == '#exit'){
			var scripts = document.getElementsByTagName('script');
			for(var i = 0;i<scripts.length;i++){
				if(scripts[i]['src'] && scripts[i]['src'].indexOf('rosy_bookmarklet.js')>0){
					document.body.removeChild(scripts[i]);
				}
			}
			document.body.removeChild(document.getElementById('rosyLogWindow'));
		}
		return true;
	},
	helpmessage : {
		"commands":{
			"#list":"show input command list"
			,"#help":"show help messages"
			,"#img":"show img file list"
			,"#css":"show external css file list"
			,"#js":"show external javascript file list"
			,"#clear":"clear console"
			,"#exit":"hide rosy log console"
		},
		"key operation":{
			"up / down key":"show input command log"
			,"left / right key":"easy input for rlog.d() / rlog.d('') (if input line is empty)"	
		}
	},
	showImgToolTip : function(){
	},
	deleteImgToolTip : function (){
	}
}
